﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using CMS.BLL;
using CMS.BLL.Entity;

public partial class AdminX_Link_LinkAdd : System.Web.UI.UserControl
{
    private int nDefaultLanguage = Support.GetDefaultLanguage();
    protected void Page_Load(object sender, EventArgs e)
    {
        MessAlert1.HideMessage();

        if (!IsPostBack)
        {
            SetLabelLanguage();
            SetValidate();
            InitData();
        }
    }

    private void SetLabelLanguage()
    {
        lblClient.Text = MultipleLanguage.PARTNERNAME[nDefaultLanguage-1];
        lblContent.Text = MultipleLanguage.CONTENT[nDefaultLanguage-1];
        lblLink.Text = MultipleLanguage.LBL_URL[nDefaultLanguage-1];

        btnCancel.Text = MultipleLanguage.BTN_BACK[nDefaultLanguage-1];
        btnSave.Text = MultipleLanguage.BTN_SAVE[nDefaultLanguage-1];

        chkActive.Text = MultipleLanguage.LBL_ACTIVE[nDefaultLanguage-1];
        chkDisplayOrder.Text = MultipleLanguage.LBL_PRIORITY[nDefaultLanguage - 1];
        lblExampleLink.Text = MultipleLanguage.ExampleLink[nDefaultLanguage - 1];
    }

    #region InitData
    private void SetValidate()
    {
        rfvTitle.ErrorMessage = MessagesValidate.ADV_CLIENT_EMPTY;
        rfvLink.ErrorMessage = MessagesValidate.ADV_PATH_EMPTY;
    }

    private void InitData()
    {
        string sQuerry = ConvertData.ConvertToString(Request.QueryString[Config.ACTION]);
        int nAdvID = ConvertData.ConvertToInt(Request.QueryString[Config.ACTION_ID]);
        try
        {
            if (sQuerry == Config.ACTION_EDIT && nAdvID > 0)
            {
                int nItemID = ConvertData.ConvertToInt(Request.QueryString[Config.ACTION_ID]);
                if (nItemID > 0)
                    GetData(nItemID);
            }
         
        }
        catch
        {
            string sRedirect = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_LINK  + "&" + Config.MESS_ID + "=" + Config.ITEM_NOTEXIST;
            Response.Redirect(sRedirect);
        }

    }

    private void GetData(int ItemID)
    {
        // Get data to view on UI Controls
        int nStatus = 0;
        Link objLink = new Link();
        objLink.LoadById(ItemID);

        txtClient.Text = objLink.Data.Name ;

        FTBContent.IntialText(ConvertData.ConvertToString(objLink.Data.Description));
        txtLink.Text = ConvertData.ConvertToString(objLink.Data.URL );
        nStatus = ConvertData.ConvertToInt(objLink.Data.Status);
        if (nStatus > 0)
            chkActive.Checked = true;
        else
            chkActive.Checked = false;

        int nPriority = ConvertData.ConvertToInt(objLink.Data.Priority);
        if (nPriority > 0)
            chkDisplayOrder.Checked = true;
        else
            chkDisplayOrder.Checked = false;

      
    }

    private eLink LinkEntity()
    {
        eLink entityLink = new eLink();
        int nItemID = ConvertData.ConvertToInt(Request.QueryString[Config.ACTION_ID]);
        string sQueery = ConvertData.ConvertToString(Request.QueryString[Config.ACTION]);
        if (nItemID > 0 && sQueery == Config.ACTION_EDIT)
        {
            Link obj = new Link();
            obj.LoadById(nItemID);
            entityLink = obj.Data;
        }
        return entityLink;
    }

    private eLink SetData()
    {
        eLink eLinkEntity = this.LinkEntity();
        Link objLink = new Link();
       
        objLink.Data.Name  = ConvertData.ConvertToString(txtClient.Text);
        objLink.Data.Description = ConvertData.ConvertToString(FTBContent.RenderText());
     
      

        if (chkActive.Checked) objLink.Data.Status = 1;
        else objLink.Data.Status = 0;

        if (chkDisplayOrder.Checked) objLink.Data.Priority = 1;
        else objLink.Data.Priority = 0;

        objLink.Data.URL = ConvertData.ConvertToString(txtLink.Text);
        objLink.Data.CreatedDate = ConvertData.ConvertDatetimeToInt(DateTime.Today);
        

        return objLink.Data;
    }
    #endregion
    private string GetQuerryString(string sQuerry)
    {
        string sQuerryString = "";
        sQuerryString = ConvertData.ConvertToString(Request.QueryString[sQuerry]);
        return sQuerryString;
    }
    #region Click Events
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        string sGoToPageMan = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_LINK ;
        Response.Redirect(sGoToPageMan);
    }



    protected void btnSave_Click1(object sender, EventArgs e)
    {
        int nActionResult = 0;
        int nDefaultLang = Support.GetDefaultLanguage() - 1;
        Link objLink = new Link();
        objLink.Data = SetData();
        string sAction = GetQuerryString(Config.ACTION);
        if (sAction == Config.ACTION_EDIT)
        {
            //Is Edit
            int nItemID = ConvertData.ConvertToInt(Request.QueryString[Config.ACTION_ID]);
            nActionResult = objLink.Update(nItemID);
            if (nActionResult > 0)
            {
                string sURL = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_LINK  + "&" + Config.MESS_ID + "=" + Config.MESSAGE_UPDATE;
                Response.Redirect(sURL);
            }
            else
                MessAlert1.ShowMessage(2, MessagesAlert.UPDATE_UNSUCCESSFUL[nDefaultLang]);
        }
        else
        {
            // Is Insert
            nActionResult = objLink.Insert();
            if (nActionResult > 0)
            {
                string sURL = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_LINK + "&" + Config.MESS_ID + "=" + Config.MESSAGE_INSERT;
                Response.Redirect(sURL);
            }

        }
    }


    #endregion

}